import os
import time
import requests
import queue
from loguru import logger
from ...utils.db import MongoDBManager

class ConsumerPool:
    def __init__(self, shared_queue: queue.Queue):
        '''
        消费者池类，用于管理多个消费者线程从共享队列中消费任务。
        '''
        self.shared_queue = shared_queue
        self.task_conut = 0
        self.db = MongoDBManager()
        self.pid = os.getpid()

    def run(self,shared_var) -> None:
        self.run_consumer(shared_var)
        

    def run_consumer(self,shared_var) -> None:
        while True:
            products = []
            time.sleep(2)
            
            try:
                while not self.shared_queue.empty() and len(products) < 1000:  # 添加批量大小限制
                    product: dict = self.shared_queue.get_nowait()  # 使用非阻塞获取
                    products.append(product)
            except queue.Empty:
                pass  # 队列为空时继续循环
                
            if products:
                original_value = shared_var.value
                shared_var.value = original_value + len(products)
                self.task_conut += len(products)
                try:
                    s = requests.session()
                    s.keep_alive = False
                    # logger.info(f"准备发送数据: {products}")
                    url = "http://192.168.112.39:25507/api/v1/bulk-insert"
                    response = s.post(url, json=products, timeout=15)
                except Exception as e:
                    logger.error(f"发送数据失败: {str(e)}")


